#!/usr/bin/perl

use Getopt::Std;

$config_file = "lsl_test.cfg";

# default settings
$repo = "https://damsl.cis.udel.edu/svn/lsl"; 
$verbose = 0;
$configure_args = " --enable-debug --enable-transparency ";
$make_args = " ";
$psd_host = "moonshine";
$psd_port = 11011;
$lsrv_host = "scotch";
$lsrv_port = 5002;
$lsrv_args = " -t 1000000000 ";
$lget_args = " -d 1 ";

sub usage()
{
	print << "EOF";
usage: $0 [-f config] [-v] [-r repo] [-h]

-r repo : specify the subversion repo to pull from
-f file : specify the config file for this run
-v	: verbose
-h	: help

EOF

	exit;
}

sub read_config {
	open(CONFIG, "$config_file");
	$config = <CONFIG>;
	eval($config);
	close(CONFIG);
}

getopts('vhf:r:', \%opt) || usage();

usage() if $opt{h};

$start = shift;
$end = shift;

# if a file has been specified read from that
if ($opt{f}) {
	$config_file = $opt{f};
}

read_config();

if ($opt{v}) {
	$verbose = 1;
}

# set the repo if the option is specified
if ($opt{r}) {
	$repo = $opt{r};
}

if ($end eq "") {
	if ($start =~ /^(\d+)\+$/ || $start eq "") {
		# get the current revision
		open(REPO_INFO, "svn info $repo | ");
		while(<REPO_INFO>) {
			if (/Revision: (\d+)/) {
				$curr_rev = $1;
			}
		}

		if ($start eq "") {
			$start = $curr_rev;
		} else {
			$start = $1;
		}

		$end = $curr_rev;
	} else {
		$end = $start;
	}
}

print "Repository: $repo\n";
print "-revisions: $start-$end\n";
if ($verbose) {
	print "-verbose\n";
}

if ($start > $end) {
	print "Error: $start > $end\n";
	exit(-1);
}

open(LOG, ">psd.$start-$end.log") || die("Couldn't open log file\n");

for $i ($start .. $end) {
	print LOG "************\n";
	print LOG "* RUN: $i *\n";
	print LOG "************\n";
	print LOG localtime()."\n";
	print LOG "************\n";

	print "Checking out revision $i\n";
	if ($verbose) {
		system("svn co -r $i $repo");
	} else {
		system("svn co -r $i $repo &> /dev/null");
	}
	$svn_result = $?;
	if ($configure_result != 0) {
		print LOG "Error($svn_result): subversion checkout failed\n";
		print "Error($svn_result): subversion checkout failed\n";
		next;
	}

	chdir("lsl/lsl") || next;

	print "Running configure\n";
	if ($verbose) {
		system("./configure $configure_args");
	} else {
		system("./configure $configure_args &> /dev/null");
	}
	$configure_result = $?;
	if ($configure_result != 0) {
		print LOG "Error($configure_result): configure failed\n";
		print "Error($configure_result): configure failed\n";
		chdir("../..");
		next;
	}

	print "Preparing source\n";
	if ($verbose) {
		system("make clean");
	} else {
		system("make clean &> /dev/null");
	}

	print "Running make\n";
	if ($verbose) {
		system("make $make_args");
	} else {
		system("make $make_args &> /dev/null");
	}
	$make_result = $?;
	if ($make_result != 0) {
		print LOG "Error($make_result): make failed\n";
		print "Error($make_result): make failed\n";
		chdir("../..");
		next;
	}

	print "Killing any existing lsrv on $lsrv_host\n";
	if ($verbose) {
		system("ssh $lsrv_host killall ./lsrv");
	} else {
		system("ssh $lsrv_host killall ./lsrv &> /dev/null");
	}

	print "Killing any existing psd on $psd_host\n";
	if ($verbose) {
		system("ssh $psd_host killall psd");
	} else {
		system("ssh $psd_host killall psd &> /dev/null");
	}

	print "Copying lsrv to $lsrv_host\n";
	if ($verbose) {
		system("scp test/lsrv $lsrv_host:");
	} else {
		system("scp test/lsrv $lsrv_host: &> /dev/null");
	}
	if ($? != 0) {
		print LOG "Error: copy of lsrv to $lsrv_host failed\n";
		print "Error: copy of lsrv to $lsrv_host failed\n";
		chdir("../..");
		next;
	}

	print "Copying psd to $psd_host\n";
	if ($verbose) {
		system("scp psd/psd $psd_host:");
	} else {
		system("scp psd/psd $psd_host: &> /dev/null");
	}
	if ($? != 0) {
		print LOG "Error: copy of psd to $psd_host failed\n";
		print "Error: copy of psd to $psd_host failed\n";
		chdir("../..");
		next;
	}

	# spawn off the lsrv
	print "Spawning lsrv on $lsrv_host\n";
	$pid = fork();
	if ($pid == 0) {
		if ($verbose) {
			exec("ssh $lsrv_host ./lsrv $lsrv_args");
		} else {
			exec("ssh $lsrv_host ./lsrv $lsrv_args &> /dev/null");
		}
		exit(0);
	} else {
		$lsrv_pid = $pid;
	}

	sleep(5);

	# spawn off the depot
	print "Spawning psd on $psd_host\n";
	$pid = fork();
	if ($pid == 0) {
		if ($verbose) {
			exec("ssh $psd_host ./psd");
		} else {
			exec("ssh $psd_host ./psd &> /dev/null");
		}
		exit(0);
	} else {
		$psd_pid = $pid;
	}

	sleep(5);

	print "Running lget\n";
	open(LGET_OUTPUT, "test/lget $lget_args $psd_host/$psd_port $lsrv_host/$lsrv_port | ");
	while(<LGET_OUTPUT>) {
		print LOG $_;
		if ($verbose) {
			print $_;
		}
	}

	print LOG "************\n";
	print LOG localtime()."\n";
	print LOG "************\n";

	kill 9, $lsrv_pid;
	kill 9, $psd_pid;
	chdir("../..");
}
